package com.fighYo.trace.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fighYo.trace.entity.Product;
import com.fighYo.trace.entity.Species;
import com.fighYo.trace.service.ProductService;
import com.fighYo.trace.service.SpeciesService;
import com.fighYo.trace.utils.R;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fighYo
 * @since 2022-04-14
 */
@Slf4j
@CrossOrigin
@Api(tags = {"产品种类"})
@RestController
@RequestMapping("/species")
public class SpeciesController {

    @Autowired
    private SpeciesService speciesService;

    @Autowired
    private ProductService productService;

    @GetMapping("list")
    private R getSpeciesList() {
        return R.ok().data("list", speciesService.list(null));
    }

    @GetMapping("get")
    private R getSpecies(String id) {
        Species species = speciesService.getById(id);
        return R.ok().data("item", species);
    }

    @PostMapping("add")
    private R addSpecies(@RequestBody Species species) {
        boolean result = speciesService.save(species);
        return result ? R.ok() : R.error();
    }

    @PostMapping("update")
    private R updateSpecies(@RequestBody Species species) {
        try {
            Species oldSpecies = speciesService.getById(species.getId());
            speciesService.updateById(species);
            Integer id = species.getId();
            String speciesName = species.getSpeciesName();
            Integer oldYield = oldSpecies.getYield();
            Integer newYield = species.getYield();

            QueryWrapper<Product> productWrapper = new QueryWrapper<>();
            productWrapper.eq("species_id", id);
            List<Product> list = productService.list(productWrapper);
            List<Product> productList = list.stream().map(product -> {
                product.setProductName(speciesName);
                Integer newQuantity = null;
                if (oldYield == null || product.getQuantity() == null || product.getQuantity() == -1) {
                    newQuantity = newYield * product.getArea();
                } else {
                    newQuantity = product.getQuantity() + product.getArea() * (newYield - oldYield);
                }
                product.setQuantity(newQuantity < 0 ? 0 : newQuantity);
                return product;
            }).collect(Collectors.toList());
            for (Product product : productList) {
                productService.updateById(product);
            }
            return R.ok();
        } catch (Exception ex) {
            log.info("修改特产失败: {}", ex);
            return R.error().msg("修改特产失败");
        }
    }

    @PostMapping("delete")
    private R deleteSpecies(@RequestBody Species species) {
        try {
            Integer id = species.getId();
            QueryWrapper<Product> productWrapper = new QueryWrapper<>();
            productWrapper.eq("species_id", id);
            List<Product> list = productService.list(productWrapper);
            if (list == null || list.size() == 0) {
                boolean result = speciesService.removeById(id);
                return result ? R.ok() : R.error().msg("删除特产失败");
            }
            return R.error().msg("删除特产失败");
        } catch (Exception ex) {
            log.info("删除特产失败: {}", ex);
            return R.error().msg("删除特产失败");
        }
    }
}

